﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;

namespace lcmyjobs.Web
{
    public partial class popup_userlogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }


        private void LoadData()
        {
            //InitCaptchaCode();
        }

        /// <summary>
        /// 初始化验证码
        /// </summary>
        private void InitCaptchaCode()
        {
            // 创建一个 6 位的随机数并保存在 Session 对象中
            //Session["CaptchaImageText"] = GenerateRandomCode();
            //imgCaptcha.ImageUrl = "~/basic/captcha/captcha.ashx?w=150&h=30&t=" + DateTime.Now.Ticks;
            
            string s = String.Empty;
            Random random = new Random();
            for (int i = 0; i < 4; i++)
            {
                s += random.Next(10).ToString();
            }
            //imgCaptcha.ImageUrl = "img.aspx?Num=" + s;
              
        }

        /// <summary>
        /// 创建一个 6 位的随机数
        /// </summary>
        /// <returns></returns>
        private string GenerateRandomCode()
        {
            string s = String.Empty;
            Random random = new Random();
            for (int i = 0; i < 6; i++)
            {
                s += random.Next(10).ToString();
            }
            return s;
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            //InitCaptchaCode();
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            /*
            if (tbxCaptcha.Text != Session["CaptchaImageText"].ToString())
            {
                Alert.ShowInTop("验证码错误！");
                return;
            }

            if (tbxUserName.Text == "admin" && tbxPassword.Text == "admin")
            {
                Alert.ShowInTop("成功登录！");
            }
            else
            {
                Alert.ShowInTop("用户名或密码错误！", MessageBoxIcon.Error);
            }
             */
            // add by liuhui 如果企业登录则个人不能登录
            if (Session["enterprise_bid"] != null)
            {
                Maticsoft.Common.MessageBox.Show(Page, "您已经登录单位账号。请先退出，才能登录个人账号！");
                return;
            }
            string name = this.tbxUserName.Text.Trim();
            string pwd = this.tbxPassword.Text.Trim();
            //string checkcode = this.tbxCaptcha.Text.Trim();
            //if (Session["CheckCode"] != null)
            //{
               // if (checkcode.ToUpper() == Session["CheckCode"].ToString())
               // {
                    lcmyjobs.BLL.user_basic bad = new BLL.user_basic();
                    string mname = "";
                    string time = "";
                    pwd = myPublic.GetMd5(pwd);
                    int id = bad.UserLogin(name, pwd, out mname, out time);
                    if (id != 0)
                    {
                        Session["user_id"] = id.ToString();
                        Session["user_name"] = mname;
                        Session["user_time"] = time;
                        //Response.Redirect("user_main.html");
                        PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
                    }
                    else
                    {
                        //Maticsoft.Common.MessageBox.Show(this, "用户名或密码不对！");
                        Alert.ShowInTop("用户名或密码错误！", MessageBoxIcon.Error);
                    }
            /*
                }
                else
                {
                    //Maticsoft.Common.MessageBox.Show(this, "验证码不对！");
                    Alert.ShowInTop("验证码错误！");
                }
            }
            else
            {
                //Maticsoft.Common.MessageBox.Show(this, "验证码不对！");
                Alert.ShowInTop("验证码错误！");
            }
            */
        }

        protected void btnreg_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript("window.location='../users/reg_name.html'");
            
        }

        protected void inputpass(object sender, EventArgs e)
        {
            //FineUI.TextMode tm = (FineUI.TextMode)TextBoxMode.Password;
            if (tbxPassword.Text != "")
            {
                tbxPassword.TextMode = (FineUI.TextMode)TextBoxMode.Password;
            }
            else
            {
                tbxPassword.TextMode = (FineUI.TextMode)TextBoxMode.SingleLine;
            }
        }
    }
}